package config

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/mitchellh/mapstructure"
	"idle/utils/configUtls"
	"log"
)

// S-速度挡位表.xlsx
type SpeedGearConfig struct {
	Sid              int    `json:"Sid"`              // Sid
	AttackSpeedRange string `json:"AttackSpeedRange"` // 攻击速度范围
	CastSpeedRange   string `json:"CastSpeedRange"`   // 施法速度范围
	AddSecend        int
}

var speedGearConfigList []*SpeedGearConfig
var speedGearConfigMap map[int]*SpeedGearConfig

func GetSpeedGearConfig(Sid int) *SpeedGearConfig {
	if v, ok := speedGearConfigMap[Sid]; ok {
		return v
	}
	return nil
}

func GetAllSpeedGearConfig() []*SpeedGearConfig {
	result := make([]*SpeedGearConfig, 0)
	for _, c := range speedGearConfigList {
		result = append(result, c)
	}
	return result
}

func loadSpeedGearConfig(filePath string) {
	xlsx, err := excelize.OpenFile(filePath)
	if err != nil {
		log.Fatal(err)
	}
	sheetName := xlsx.GetSheetName(1)
	rows := xlsx.GetRows(sheetName)

	speedGearConfigMap = make(map[int]*SpeedGearConfig)
	speedGearConfigList = make([]*SpeedGearConfig, 0)

	for _, row := range rows[4:] {
		data := make(map[string]interface{})
		for j, colCell := range row {
			fieldName := rows[1][j]
			if fieldName != "" {
				data[rows[1][j]] = configUtls.GetValueByType(rows[2][j], colCell)
			}
		}
		var config SpeedGearConfig
		err := mapstructure.Decode(data, &config)
		if err != nil {
			log.Fatal(err)
			return
		}

		speedGearConfigMap[config.Sid] = &config
		speedGearConfigList = append(speedGearConfigList, &config)
	}
}
